import register_colour from './javascript/colour'
import register_lists from './javascript/lists'
import register_logic from './javascript/logic'
import register_loops from './javascript/loops'
import register_math from './javascript/math'
import register_math_scratch from './javascript/math_scratch'
import register_procedures from './javascript/procedures'
import register_text from './javascript/text'
import register_data from './javascript/data'
import register_variables from './javascript/variables'
import register_variables_dynamic from './javascript/variables_dynamic'
import register_operator from './javascript/operator'

export default function() {

  /**
   * JavaScript code generator.
   * @type {!Blockly.Generator}
   */
  let coder = new Blockly.Generator('JavaScript');

  coder.indent = 0
  coder.indentString = function(){
    return "\t".repeat(this.indent||0)
  }

  let tmpVarIdx = 0
  coder.defineTmpVar = function() {
    return `_$TMPVAR${tmpVarIdx++}_`
  }

  /**
   * List of illegal variable names.
   * This is not intended to be a security feature.  Blockly is 100% client-side,
   * so bypassing this list is trivial.  This is intended to prevent users from
   * accidentally clobbering a built-in object or function.
   * @private
   */
  coder.addReservedWords(
      'Blockly,' +  // In case JS is evaled in the current window.
      // https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words
      'break,case,catch,continue,debugger,default,delete,do,else,finally,for,function,if,in,instanceof,new,return,switch,this,throw,try,typeof,var,void,while,with,' +
      'class,enum,export,extends,import,super,implements,interface,let,package,private,protected,public,static,yield,' +
      'const,null,true,false,' +
      // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects
      'Array,ArrayBuffer,Boolean,Date,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,Error,eval,EvalError,Float32Array,Float64Array,Function,Infinity,Int16Array,Int32Array,Int8Array,isFinite,isNaN,Iterator,JSON,Math,NaN,Number,Object,parseFloat,parseInt,RangeError,ReferenceError,RegExp,StopIteration,String,SyntaxError,TypeError,Uint16Array,Uint32Array,Uint8Array,Uint8ClampedArray,undefined,uneval,URIError,' +
      // https://developer.mozilla.org/en/DOM/window
      'applicationCache,closed,Components,content,_content,controllers,crypto,defaultStatus,dialogArguments,directories,document,frameElement,frames,fullScreen,globalStorage,history,innerHeight,innerWidth,length,location,locationbar,localStorage,menubar,messageManager,mozAnimationStartTime,mozInnerScreenX,mozInnerScreenY,mozPaintCount,name,navigator,opener,outerHeight,outerWidth,pageXOffset,pageYOffset,parent,performance,personalbar,pkcs11,returnValue,screen,screenX,screenY,scrollbars,scrollMaxX,scrollMaxY,scrollX,scrollY,self,sessionStorage,sidebar,status,statusbar,toolbar,top,URL,window,' +
      'addEventListener,alert,atob,back,blur,btoa,captureEvents,clearImmediate,clearInterval,clearTimeout,close,confirm,disableExternalCapture,dispatchEvent,dump,enableExternalCapture,escape,find,focus,forward,GeckoActiveXObject,getAttention,getAttentionWithCycleCount,getComputedStyle,getSelection,home,matchMedia,maximize,minimize,moveBy,moveTo,mozRequestAnimationFrame,open,openDialog,postMessage,print,prompt,QueryInterface,releaseEvents,removeEventListener,resizeBy,resizeTo,restore,routeEvent,scroll,scrollBy,scrollByLines,scrollByPages,scrollTo,setCursor,setImmediate,setInterval,setResizable,setTimeout,showModalDialog,sizeToContent,stop,unescape,updateCommands,XPCNativeWrapper,XPCSafeJSObjectWrapper,' +
      'onabort,onbeforeunload,onblur,onchange,onclick,onclose,oncontextmenu,ondevicemotion,ondeviceorientation,ondragdrop,onerror,onfocus,onhashchange,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onmozbeforepaint,onpaint,onpopstate,onreset,onresize,onscroll,onselect,onsubmit,onunload,onpageshow,onpagehide,' +
      'Image,Option,Worker,' +
      // https://developer.mozilla.org/en/Gecko_DOM_Reference
      'Event,Range,File,FileReader,Blob,BlobBuilder,' +
      'Attr,CDATASection,CharacterData,Comment,console,DocumentFragment,DocumentType,DomConfiguration,DOMError,DOMErrorHandler,DOMException,DOMImplementation,DOMImplementationList,DOMImplementationRegistry,DOMImplementationSource,DOMLocator,DOMObject,DOMString,DOMStringList,DOMTimeStamp,DOMUserData,Entity,EntityReference,MediaQueryList,MediaQueryListListener,NameList,NamedNodeMap,Node,NodeFilter,NodeIterator,NodeList,Notation,Plugin,PluginArray,ProcessingInstruction,SharedWorker,Text,TimeRanges,Treewalker,TypeInfo,UserDataHandler,Worker,WorkerGlobalScope,' +
      'HTMLDocument,HTMLElement,HTMLAnchorElement,HTMLAppletElement,HTMLAudioElement,HTMLAreaElement,HTMLBaseElement,HTMLBaseFontElement,HTMLBodyElement,HTMLBRElement,HTMLButtonElement,HTMLCanvasElement,HTMLDirectoryElement,HTMLDivElement,HTMLDListElement,HTMLEmbedElement,HTMLFieldSetElement,HTMLFontElement,HTMLFormElement,HTMLFrameElement,HTMLFrameSetElement,HTMLHeadElement,HTMLHeadingElement,HTMLHtmlElement,HTMLHRElement,HTMLIFrameElement,HTMLImageElement,HTMLInputElement,HTMLKeygenElement,HTMLLabelElement,HTMLLIElement,HTMLLinkElement,HTMLMapElement,HTMLMenuElement,HTMLMetaElement,HTMLModElement,HTMLObjectElement,HTMLOListElement,HTMLOptGroupElement,HTMLOptionElement,HTMLOutputElement,HTMLParagraphElement,HTMLParamElement,HTMLPreElement,HTMLQuoteElement,HTMLScriptElement,HTMLSelectElement,HTMLSourceElement,HTMLSpanElement,HTMLStyleElement,HTMLTableElement,HTMLTableCaptionElement,HTMLTableCellElement,HTMLTableDataCellElement,HTMLTableHeaderCellElement,HTMLTableColElement,HTMLTableRowElement,HTMLTableSectionElement,HTMLTextAreaElement,HTMLTimeElement,HTMLTitleElement,HTMLTrackElement,HTMLUListElement,HTMLUnknownElement,HTMLVideoElement,' +
      'HTMLCanvasElement,CanvasRenderingContext2D,CanvasGradient,CanvasPattern,TextMetrics,ImageData,CanvasPixelArray,HTMLAudioElement,HTMLVideoElement,NotifyAudioAvailableEvent,HTMLCollection,HTMLAllCollection,HTMLFormControlsCollection,HTMLOptionsCollection,HTMLPropertiesCollection,DOMTokenList,DOMSettableTokenList,DOMStringMap,RadioNodeList,' +
      'SVGDocument,SVGElement,SVGAElement,SVGAltGlyphElement,SVGAltGlyphDefElement,SVGAltGlyphItemElement,SVGAnimationElement,SVGAnimateElement,SVGAnimateColorElement,SVGAnimateMotionElement,SVGAnimateTransformElement,SVGSetElement,SVGCircleElement,SVGClipPathElement,SVGColorProfileElement,SVGCursorElement,SVGDefsElement,SVGDescElement,SVGEllipseElement,SVGFilterElement,SVGFilterPrimitiveStandardAttributes,SVGFEBlendElement,SVGFEColorMatrixElement,SVGFEComponentTransferElement,SVGFECompositeElement,SVGFEConvolveMatrixElement,SVGFEDiffuseLightingElement,SVGFEDisplacementMapElement,SVGFEDistantLightElement,SVGFEFloodElement,SVGFEGaussianBlurElement,SVGFEImageElement,SVGFEMergeElement,SVGFEMergeNodeElement,SVGFEMorphologyElement,SVGFEOffsetElement,SVGFEPointLightElement,SVGFESpecularLightingElement,SVGFESpotLightElement,SVGFETileElement,SVGFETurbulenceElement,SVGComponentTransferFunctionElement,SVGFEFuncRElement,SVGFEFuncGElement,SVGFEFuncBElement,SVGFEFuncAElement,SVGFontElement,SVGFontFaceElement,SVGFontFaceFormatElement,SVGFontFaceNameElement,SVGFontFaceSrcElement,SVGFontFaceUriElement,SVGForeignObjectElement,SVGGElement,SVGGlyphElement,SVGGlyphRefElement,SVGGradientElement,SVGLinearGradientElement,SVGRadialGradientElement,SVGHKernElement,SVGImageElement,SVGLineElement,SVGMarkerElement,SVGMaskElement,SVGMetadataElement,SVGMissingGlyphElement,SVGMPathElement,SVGPathElement,SVGPatternElement,SVGPolylineElement,SVGPolygonElement,SVGRectElement,SVGScriptElement,SVGStopElement,SVGStyleElement,SVGSVGElement,SVGSwitchElement,SVGSymbolElement,SVGTextElement,SVGTextPathElement,SVGTitleElement,SVGTRefElement,SVGTSpanElement,SVGUseElement,SVGViewElement,SVGVKernElement,' +
      'SVGAngle,SVGColor,SVGICCColor,SVGElementInstance,SVGElementInstanceList,SVGLength,SVGLengthList,SVGMatrix,SVGNumber,SVGNumberList,SVGPaint,SVGPoint,SVGPointList,SVGPreserveAspectRatio,SVGRect,SVGStringList,SVGTransform,SVGTransformList,' +
      'SVGAnimatedAngle,SVGAnimatedBoolean,SVGAnimatedEnumeration,SVGAnimatedInteger,SVGAnimatedLength,SVGAnimatedLengthList,SVGAnimatedNumber,SVGAnimatedNumberList,SVGAnimatedPreserveAspectRatio,SVGAnimatedRect,SVGAnimatedString,SVGAnimatedTransformList,' +
      'SVGPathSegList,SVGPathSeg,SVGPathSegArcAbs,SVGPathSegArcRel,SVGPathSegClosePath,SVGPathSegCurvetoCubicAbs,SVGPathSegCurvetoCubicRel,SVGPathSegCurvetoCubicSmoothAbs,SVGPathSegCurvetoCubicSmoothRel,SVGPathSegCurvetoQuadraticAbs,SVGPathSegCurvetoQuadraticRel,SVGPathSegCurvetoQuadraticSmoothAbs,SVGPathSegCurvetoQuadraticSmoothRel,SVGPathSegLinetoAbs,SVGPathSegLinetoHorizontalAbs,SVGPathSegLinetoHorizontalRel,SVGPathSegLinetoRel,SVGPathSegLinetoVerticalAbs,SVGPathSegLinetoVerticalRel,SVGPathSegMovetoAbs,SVGPathSegMovetoRel,ElementTimeControl,TimeEvent,SVGAnimatedPathData,' +
      'SVGAnimatedPoints,SVGColorProfileRule,SVGCSSRule,SVGExternalResourcesRequired,SVGFitToViewBox,SVGLangSpace,SVGLocatable,SVGRenderingIntent,SVGStylable,SVGTests,SVGTextContentElement,SVGTextPositioningElement,SVGTransformable,SVGUnitTypes,SVGURIReference,SVGViewSpec,SVGZoomAndPan');

  /**
   * Order of operation ENUMs.
   * https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
   */
  coder.ORDER_ATOMIC = 0;           // 0 "" ...
  coder.ORDER_NEW = 1.1;            // new
  coder.ORDER_MEMBER = 1.2;         // . []
  coder.ORDER_FUNCTION_CALL = 2;    // ()
  coder.ORDER_INCREMENT = 3;        // ++
  coder.ORDER_DECREMENT = 3;        // --
  coder.ORDER_BITWISE_NOT = 4.1;    // ~
  coder.ORDER_UNARY_PLUS = 4.2;     // +
  coder.ORDER_UNARY_NEGATION = 4.3; // -
  coder.ORDER_LOGICAL_NOT = 4.4;    // !
  coder.ORDER_TYPEOF = 4.5;         // typeof
  coder.ORDER_VOID = 4.6;           // void
  coder.ORDER_DELETE = 4.7;         // delete
  coder.ORDER_AWAIT = 4.8;          // await
  coder.ORDER_EXPONENTIATION = 5.0; // **
  coder.ORDER_MULTIPLICATION = 5.1; // *
  coder.ORDER_DIVISION = 5.2;       // /
  coder.ORDER_MODULUS = 5.3;        // %
  coder.ORDER_SUBTRACTION = 6.1;    // -
  coder.ORDER_ADDITION = 6.2;       // +
  coder.ORDER_BITWISE_SHIFT = 7;    // << >> >>>
  coder.ORDER_RELATIONAL = 8;       // < <= > >=
  coder.ORDER_IN = 8;               // in
  coder.ORDER_INSTANCEOF = 8;       // instanceof
  coder.ORDER_EQUALITY = 9;         // == != === !==
  coder.ORDER_BITWISE_AND = 10;     // &
  coder.ORDER_BITWISE_XOR = 11;     // ^
  coder.ORDER_BITWISE_OR = 12;      // |
  coder.ORDER_LOGICAL_AND = 13;     // &&
  coder.ORDER_LOGICAL_OR = 14;      // ||
  coder.ORDER_CONDITIONAL = 15;     // ?:
  coder.ORDER_ASSIGNMENT = 16;      // = += -= **= *= /= %= <<= >>= ...
  coder.ORDER_YIELD = 17;         // yield
  coder.ORDER_COMMA = 18;           // ,
  coder.ORDER_NONE = 99;            // (...)

  /**
   * List of outer-inner pairings that do NOT require parentheses.
   * @type {!Array.<!Array.<number>>}
   */
  coder.ORDER_OVERRIDES = [
    // (foo()).bar -> foo().bar
    // (foo())[0] -> foo()[0]
    [coder.ORDER_FUNCTION_CALL, coder.ORDER_MEMBER],
    // (foo())() -> foo()()
    [coder.ORDER_FUNCTION_CALL, coder.ORDER_FUNCTION_CALL],
    // (foo.bar).baz -> foo.bar.baz
    // (foo.bar)[0] -> foo.bar[0]
    // (foo[0]).bar -> foo[0].bar
    // (foo[0])[1] -> foo[0][1]
    [coder.ORDER_MEMBER, coder.ORDER_MEMBER],
    // (foo.bar)() -> foo.bar()
    // (foo[0])() -> foo[0]()
    [coder.ORDER_MEMBER, coder.ORDER_FUNCTION_CALL],

    // !(!foo) -> !!foo
    [coder.ORDER_LOGICAL_NOT, coder.ORDER_LOGICAL_NOT],
    // a * (b * c) -> a * b * c
    [coder.ORDER_MULTIPLICATION, coder.ORDER_MULTIPLICATION],
    // a + (b + c) -> a + b + c
    [coder.ORDER_ADDITION, coder.ORDER_ADDITION],
    // a && (b && c) -> a && b && c
    [coder.ORDER_LOGICAL_AND, coder.ORDER_LOGICAL_AND],
    // a || (b || c) -> a || b || c
    [coder.ORDER_LOGICAL_OR, coder.ORDER_LOGICAL_OR]
  ];

  /**
   * Initialise the database of variable names.
   * @param {!Blockly.Workspace} workspace Workspace to generate code from.
   */
  coder.init = function(workspace: any) {

    tmpVarIdx = 0

    // // Create a dictionary of definitions to be printed before the code.
    // coder.definitions_ = Object.create(null);
    // // Create a dictionary mapping desired function names in definitions_
    // // to actual function names (to avoid collisions with user functions).
    // coder.functionNames_ = Object.create(null);

    // if (!coder.variableDB_) {
    //   coder.variableDB_ =
    //       new Blockly.Names(coder.RESERVED_WORDS_);
    // } else {
    //   coder.variableDB_.reset();
    // }

    // coder.variableDB_.setVariableMap(workspace.getVariableMap());

    // var defvars = [];
    // // Add developer variables (not created or named by the user).
    // var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
    // for (var i = 0; i < devVarList.length; i++) {
    //   defvars.push(coder.variableDB_.getName(devVarList[i],
    //       Blockly.Names.DEVELOPER_VARIABLE_TYPE));
    // }

    // // Add user variables, but only ones that are being used.
    // var variables = Blockly.Variables.allUsedVarModels(workspace);
    // for (var i = 0; i < variables.length; i++) {
    //   defvars.push(coder.variableDB_.getName(variables[i].getId(),
    //       Blockly.Variables.NAME_TYPE));
    // }

    // // Declare all of the variables.
    // if (defvars.length) {
    //   coder.definitions_['variables'] =
    //       'var ' + defvars.join(', ') + ';';
    // }
  };

  /**
   * Prepend the generated code with the variable definitions.
   * @param {string} code Generated code.
   * @return {string} Completed code.
   */
  // coder.finish = function(code: string) {
  //   // Convert the definitions dictionary into a list.
  //   var definitions = [];
  //   for (var name in coder.definitions_) {
  //     definitions.push(coder.definitions_[name]);
  //   }
  //   // Clean up temporary data.
  //   delete coder.definitions_;
  //   delete coder.functionNames_;
  //   coder.variableDB_.reset();
  //   return definitions.join('\n\n') + '\n\n\n' + code;
  // };

  /**
   * Naked values are top-level blocks with outputs that aren't plugged into
   * anything.  A trailing semicolon is needed to make this legal.
   * @param {string} line Line of generated code.
   * @return {string} Legal line of code.
   */
  coder.scrubNakedValue = function(line: string) {
    return line + ';\n';
  };

  /**
   * Encode a string as a properly escaped JavaScript string, complete with
   * quotes.
   * @param {string} string Text to encode.
   * @return {string} JavaScript string.
   * @private
   */
  coder.quote_ = function(string: string) {
    // Can't use goog.string.quote since Google's style guide recommends
    // JS string literals use single quotes.
    string = string.replace(/\\/g, '\\\\')
                  .replace(/\n/g, '\\\n')
                  .replace(/'/g, '\\\'');
    return '\'' + string + '\'';
  };

  /**
   * Common tasks for generating JavaScript from blocks.
   * Handles comments for the specified block and any connected value blocks.
   * Calls any statements following this block.
   * @param {!Blockly.Block} block The current block.
   * @param {string} code The JavaScript code created for this block.
   * @param {boolean=} opt_thisOnly True to generate code for only this statement.
   * @return {string} JavaScript code with comments and subsequent blocks added.
   * @private
   */
  coder.scrub_ = function(block: any, code: string, opt_thisOnly: string) {
    var commentCode = '';
    // Only collect comments for blocks that aren't inline.
    if (!block.outputConnection || !block.outputConnection.targetConnection) {
      // Collect comment for this block.
      var comment = block.getCommentText();
      comment = Blockly.utils.wrap(comment, coder.COMMENT_WRAP - 3);
      if (comment) {
        if (block.getProcedureDef) {
          // Use a comment block for function comments.
          commentCode += '/**\n' +
                        coder.prefixLines(comment + '\n', ' * ') +
                        ' */\n';
        } else {
          commentCode += coder.prefixLines(comment + '\n', '// ');
        }
      }
      // Collect comments for all value arguments.
      // Don't collect comments for nested statements.
      for (var i = 0; i < block.inputList.length; i++) {
        if (block.inputList[i].type == Blockly.INPUT_VALUE) {
          var childBlock = block.inputList[i].connection.targetBlock();
          if (childBlock) {
            var comment = coder.allNestedComments(childBlock);
            if (comment) {
              commentCode += coder.prefixLines(comment, '// ');
            }
          }
        }
      }
    }

    if(block.isEventHandleFunction) {
      return code
    }

    var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
    var nextCode = opt_thisOnly ? '' : coder.blockToCode(nextBlock);
    return commentCode + code + nextCode;
  };

  /**
   * Gets a property and adjusts the value while taking into account indexing.
   * @param {!Blockly.Block} block The block.
   * @param {string} atId The property ID of the element to get.
   * @param {number=} opt_delta Value to add.
   * @param {boolean=} opt_negate Whether to negate the value.
   * @param {number=} opt_order The highest order acting on this value.
   * @return {string|number}
   */
  coder.getAdjusted = function(block: any, atId: string, opt_delta: number, opt_negate: boolean,
      opt_order: number) {
    var delta = opt_delta || 0;
    var order = opt_order || coder.ORDER_NONE;
    if (block.workspace.options.oneBasedIndex) {
      delta--;
    }
    var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
    if (delta > 0) {
      var at = coder.valueToCode(block, atId,
          coder.ORDER_ADDITION) || defaultAtIndex;
    } else if (delta < 0) {
      var at = coder.valueToCode(block, atId,
          coder.ORDER_SUBTRACTION) || defaultAtIndex;
    } else if (opt_negate) {
      var at = coder.valueToCode(block, atId,
          coder.ORDER_UNARY_NEGATION) || defaultAtIndex;
    } else {
      var at = coder.valueToCode(block, atId, order) ||
          defaultAtIndex;
    }

    if (Blockly.isNumber(at)) {
      // If the index is a naked number, adjust it right now.
      at = parseFloat(at) + delta;
      if (opt_negate) {
        at = -at;
      }
    } else {
      // If the index is dynamic, adjust it in code.
      if (delta > 0) {
        at = at + ' + ' + delta;
        var innerOrder = coder.ORDER_ADDITION;
      } else if (delta < 0) {
        at = at + ' - ' + -delta;
        var innerOrder = coder.ORDER_SUBTRACTION;
      }
      if (opt_negate) {
        if (delta) {
          at = '-(' + at + ')';
        } else {
          at = '-' + at;
        }
        var innerOrder = coder.ORDER_UNARY_NEGATION;
      }
      innerOrder = Math.floor(innerOrder);
      order = Math.floor(order);
      if (innerOrder && order >= innerOrder) {
        at = '(' + at + ')';
      }
    }
    return at;
  }

  register_colour(coder)
  register_lists(coder)
  register_logic(coder)
  register_loops(coder)
  register_math(coder)
  register_math_scratch(coder)
  register_procedures(coder)
  register_text(coder)
  register_data(coder)
  register_variables(coder)
  register_variables_dynamic(coder)
  register_variables_dynamic(coder)
  register_operator(coder)

  return coder
}
